#!/usr/bin/python
# -*- coding: utf-8 -*-

import Ice
import sys, os, thread, random

try:
    HOME = os.environ['ROBOTS']
except KeyError:
    print 'ROBOTS environment variable is not currently defined.'
    sys.exit(0)

SLICE = os.path.join(HOME, 'slice')
FILE = os.path.join(SLICE, 'Robots.ice')
Ice.loadSlice('--all -I/usr/share/slice', ['-I' + SLICE, FILE])

import Robots
import RobotI
from RobotI import *

class RobotFactoryI (Robots.RobotFactory):

    def __init__ (self):
        # Key   ==> Object identity.
        # Value ==> Servant.
        self.__robots = {}

    def create (self, addresses, curr):

        prxs = []

        for address in addresses:
            # Register the servant.
            id = curr.adapter.getCommunicator().stringToIdentity(address)
            servant = RobotI(address)
            try:
                prxs.append(Robots.RobotPrx.checkedCast(curr.adapter.add(servant, id)))
                self.__robots[id] = servant
            except Ice.AlreadyRegisteredException:
                pass

        return prxs

    # Get the sequence of agent proxies.
    def list (self, curr):
        prxs = []

        for id in self.__robots.keys():
            prxs.append(Robots.RobotPrx.uncheckedCast(curr.adapter.createProxy(id)))

        return prxs
